#include "EjMain.h"
#include <QApplication>
#include <QDesktopWidget>

#include "ejsidebar.h"

#include <QListWidget>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QToolButton>

#include <QDebug>

EjMain::EjMain(QWidget *parent)
    : QWidget(parent, Qt::FramelessWindowHint)
    , bPressFlag(false)
{
    //resize(660, 460);
    splitter = new QSplitter(Qt::Horizontal, this);
    splitter->setHandleWidth(1);
    splitter->setStyleSheet("QSplitter::handle{background:#808080}");
    contentFrame = new QFrame(splitter);
    contentFrame->setStyleSheet("background:#323232");
    //contentFrame->resize(475, height());
    //contentFrame->setMinimumWidth(405);
    listFrame = new QFrame(splitter);
    listFrame->setStyleSheet("background:#161616");
    //listFrame->resize(660 - 475, height());
    listFrame->setMaximumWidth(80);
    connect(splitter, SIGNAL(splitterMoved(int,int)), this, SLOT(slotSplitterMoved(int,int)));

    leftIcon = QIcon(":/res/png/left_arrow.png");
    rightIcon = QIcon(":/res/png/right_arrow.png");

    pushButton = new QPushButton(this);
    pushButton->setFocusPolicy(Qt::NoFocus);
    pushButton->hide();
    pushButton->setFixedSize(13, 42);
    pushButton->setIconSize(pushButton->size());
    pushButton->setStyleSheet("border:none;");
    pushButton->setIcon(rightIcon);
    pushButton->move(contentFrame->width() - pushButton->width()-2, (contentFrame->height() - pushButton->height())/2);
    connect(pushButton,SIGNAL(clicked()),this,SLOT(slotClickedBtn()));

    contentFrame->setMouseTracking(true);
    listFrame->setMouseTracking(true);
    contentFrame->installEventFilter(this);
    listFrame->installEventFilter(this);

    auto sidebar = new EjSideBar(listFrame);
    auto layout = new QVBoxLayout();
    layout->addWidget(sidebar, 0, Qt::AlignCenter);
    layout->setMargin(0);
    layout->setSpacing(0);
    listFrame->setLayout(layout);

    auto newAction = sidebar->addAction(tr("新建工程"), QIcon(":/res/png/project_new_48px.png"));
    auto openAction = sidebar->addAction(tr("打开工程"), QIcon(":/res/png/project_open_48px.png"));
    auto exsampleAction = sidebar->addAction(tr("打开例程"), QIcon(":/res/png/project_search_48px.png"));

    //connect(newAction, SIGNAL(triggered(bool)), this, SLOT(slotNew(bool)));
    connect(newAction, &QAction::triggered, []()->void{qDebug() << "test!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";});

}

EjMain::~EjMain()
{

}

void EjMain::resizeEvent(QResizeEvent *event)
{
    splitter->setGeometry(0, 0, width(), height());
    move(0,  0);
    QWidget::resizeEvent(event);
}

bool EjMain::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseMove) {
        QMouseEvent *mouseMove = static_cast<QMouseEvent*>(event);
        QRect rect = pushButton->frameGeometry();
        if (rect.contains(mouseMove->pos())) {
            pushButton->show();
        }
        else {
            pushButton->hide();
        }
    }
    return QWidget::eventFilter(obj, event);
}

void EjMain::setBtnPos()
{
    pushButton->move(contentFrame->width() - pushButton->width(), (contentFrame->height() - pushButton->height())/2);
}

void EjMain::setBtnIcon()
{
    if (listFrame->width() != 0) {
        pushButton->setIcon(rightIcon);
    }
    else {
        pushButton->setIcon(leftIcon);
    }
}

void EjMain::slotClickedBtn()
{
    QList <int> sizeList;
    sizeList.clear();
    if (listFrame->width() != 0) {
        sizeList.append(660);
        sizeList.append(0);
    }
    else {
        sizeList.append(475);
        sizeList.append(660 - 475);
    }
    splitter->setSizes(sizeList);

    setBtnIcon();
    setBtnPos();
    pushButton->hide();
}

void EjMain::slotSplitterMoved(int pos, int index)
{
    Q_UNUSED(pos)
    Q_UNUSED(index)
    setBtnIcon();
    setBtnPos();
}

void EjMain::slotNew(bool)
{
    qDebug() << "test!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}

void EjMain::mousePressEvent ( QMouseEvent * event)
{
    QWidget::mousePressEvent(event);
}

void EjMain::mouseMoveEvent(QMouseEvent *event)
{
    QWidget::mouseMoveEvent(event);
}

void EjMain::mouseReleaseEvent(QMouseEvent *event)
{
    QWidget::mouseReleaseEvent(event);
}
